package com.deep.spring.v2;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.reactive.function.client.WebClient;
import org.springframework.web.util.DefaultUriBuilderFactory;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.HashMap;
import java.util.Map;

public class UrlDemo {

    public static void main(String[] args) throws URISyntaxException {
//        v1();
        v2();
    }

    public static void v1() throws URISyntaxException {

        String baseUrl = "http://example.com";
        DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);
        RestTemplate restTemplate = new RestTemplate();

        restTemplate.setUriTemplateHandler(factory);

        Map<String, Object> defParam = new HashMap<>();


        Map<String, Object> requestor = new HashMap<>();
        defParam.put("requestor", requestor);

        HttpHeaders headers = new HttpHeaders();
        headers.add("signature", "afasdf");
        headers.add("timestamp", "timestamp");

        HttpEntity<Map<String, Object>> requestEntity = new HttpEntity<>(defParam, headers);

        URI uri = new URI("https://www.baidu.com");
        ResponseEntity<String> response = restTemplate.exchange(uri, HttpMethod.GET, requestEntity, String.class);

        System.out.println(response.getStatusCode());
        System.out.println(response.getStatusCodeValue());
        System.out.println(response.getBody());
        System.out.println(response.getHeaders());

    }

    public static void v2() {
        String baseUrl = "http://example.com";
        DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory(baseUrl);


        WebClient client1 = WebClient.builder().uriBuilderFactory(factory).build();

        WebClient client2 = WebClient.builder().baseUrl(baseUrl).build();

        WebClient client3 = WebClient.create(baseUrl);


        System.out.println(client1.get().retrieve().toBodilessEntity());
    }


}
